package org.ssp.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.ssp.base.ResultSetHanlder;
import org.ssp.dao.NewsDao;
import org.ssp.pojo.News;
import org.ssp.pojo.Travel;
import org.ssp.util.DBUtil;

public class NewsImpl implements NewsDao {

	@Override
	public void addNews(News news) {
		String sql="insert into news(title,content,counter,datetime,firstimgurl) values(?,?,0,now(),?);";
		DBUtil.update(sql, news.getTitle(),news.getContent(),news.getFirstimgurl());
		
	}

	@Override
	public void deleteNews(int id) {
		String sql="delete from news where id=?";
		DBUtil.update(sql, id);
	}

	@Override
	public void updateNews(News news) {
		String sql="update news set title=?,content=?,firstimgurl=? where id=?";
		DBUtil.update(sql, news.getTitle(),news.getContent(),news.getFirstimgurl(),news.getId());
	}

	@Override
	public ArrayList<News> findAll(int offset,int rows) {
		String sql="select * from news limit ?,?";
		return (ArrayList<News>)DBUtil.query(sql, new ResultSetHanlder(){
			@Override
			public Object doHandler(ResultSet rs) {
				ArrayList<News> news=new ArrayList<News>();

				try {
					while(rs.next()){
						News n=new News();
						n.setId(rs.getInt("id"));
						n.setTitle(rs.getString("title"));
						n.setContent(rs.getString("content"));
						n.setDatetime(rs.getDate("datetime"));
						n.setFirstimgurl(rs.getString("firstimgurl"));
						news.add(n);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
				return news;
			}
		}, offset,rows);
	}

	@Override
	public int findAll() {
		// TODO Auto-generated method stub
		String sql="select * from news;";
		return (Integer)DBUtil.query(sql, new ResultSetHanlder() {
			
			@Override
			public Object doHandler(ResultSet rs) {
				Integer total=0;
				try {
					while(rs.next()){
						total+=1;
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
				return total;
			}
		}, null);
	}
    @Override
    public ArrayList<News> find10(int offset,int rows){
    	String sql="select * from news limit ?,?;";
    	return (ArrayList<News>)DBUtil.query(sql, new ResultSetHanlder() {
			
			@Override
			public Object doHandler(ResultSet rs) {
				ArrayList<News> list=new ArrayList<News>();
				try {
					while(rs.next()){
						News news=new News();
						news.setId(rs.getInt("id"));
						news.setTitle(rs.getString("title"));
						news.setContent(rs.getString("content"));
						news.setFirstimgurl(rs.getString("firstimgurl"));
						news.setDatetime(rs.getDate("datetime"));
						list.add(news);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
				return list;
			}
		}, offset,rows);
    }

	@Override
	public News findById(int id) {
		String sql="select * from news where id=?;";
		return  (News)DBUtil.query(sql, new ResultSetHanlder() {
			
			@Override
			public Object doHandler(ResultSet rs) {
				News news=null;
				try {
					if(rs.next()){
						news=new News();
						news.setId(rs.getInt("id"));
						news.setTitle(rs.getString("title"));
						news.setContent(rs.getString("content"));
						news.setFirstimgurl(rs.getString("firstimgurl"));
						news.setDatetime(rs.getDate("datetime"));
						
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
				return news;
			}
		}, id);
	}

	@Override
	public ArrayList<News> findAllMobile() {
		String sql="select * from news order by id desc";
		return (ArrayList<News>)DBUtil.query(sql, new ResultSetHanlder(){
			@Override
			public Object doHandler(ResultSet rs) {
				ArrayList<News> news=new ArrayList<News>();

				try {
					while(rs.next()){
						News n=new News();
						n.setId(rs.getInt("id"));
						n.setTitle(rs.getString("title"));
						n.setContent(rs.getString("content"));
						n.setDatetime(rs.getDate("datetime"));
						n.setFirstimgurl(rs.getString("firstimgurl"));
						news.add(n);
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
				return news;
			}
		}, null);
	}

	@Override
	public String getContent(int id) {
		String sql="select * from  news where id=?";
		return (String)DBUtil.query(sql, new ResultSetHanlder() {
			
			@Override
			public Object doHandler(ResultSet rs) {
				String content=new String();
				try {
					if(rs.next()){
						content=rs.getString("content");
						
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return null;
				}
				return content;
			}
		}, id);
	}
}
